package io.springboot.jpa.examples.springbootjpaexamples.service;

import io.springboot.jpa.examples.springbootjpaexamples.model.Menu;
import io.springboot.jpa.examples.springbootjpaexamples.model.Role;
import io.springboot.jpa.examples.springbootjpaexamples.model.User;
import io.springboot.jpa.examples.springbootjpaexamples.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author yzc
 * @description
 * @date 2018/9/2
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Page<User> findAll(Pageable pageable) {
        return userRepository.findAll(pageable);
    }

    public User getOne(long id) {
        User user = userRepository.getOne(id);
        return user;
    }

    @Transactional
    public User save(User user) {
        return userRepository.save(user);
    }

    public List<Menu> getMenusByUser(long id) {
        User user = userRepository.getOne(id);
        List<Menu> superiorMenus = new ArrayList<>();
        List<Menu> subsetMenus = new ArrayList<>();
        Set<Role> roles = user.getRoleSet();
        Iterator<Role> iterator = roles.iterator();
        while (iterator.hasNext()) {
            Role role = iterator.next();
            List<Menu> menuList = role.getMenuList();
            for (int i=0; i< menuList.size(); i++) {
                Menu menu = menuList.get(i);
                if (menu.getParentId() == -1) {
                    if (!superiorMenus.contains(menu)) {
                        superiorMenus.add(menu);
                    }
                } else {
                    if (!subsetMenus.contains(menu)) {
                        subsetMenus.add(menu);
                    }
                }
            }
        }
        for (int i=0; i<subsetMenus.size(); i++) {
            Menu menu = subsetMenus.get(i);
            for (int j=0; j<superiorMenus.size(); j++) {
                Menu superiorMenu = superiorMenus.get(j);
                if (menu.getParentId() == superiorMenu.getId()) {
                    superiorMenu.getSubsets().add(menu);
                }
            }
        }

        Collections.sort(superiorMenus, new Comparator<Menu>() {
            @Override
            public int compare(Menu o1, Menu o2) {
                return o1.getSort() - o2.getSort();
            }
        });
        for (int j=0; j<superiorMenus.size(); j++) {
            Menu superiorMenu = superiorMenus.get(j);
            Collections.sort(superiorMenu.getSubsets(), new Comparator<Menu>() {
                @Override
                public int compare(Menu o1, Menu o2) {
                    return o1.getSort() - o2.getSort();
                }
            });
        }

        return superiorMenus;
    }
}
